<?php
/**
 * 爱奇吉AI摘要 - 前端文章加密访问控制
 *
 * @package DeepSeekAISummarizer
 */

if (!class_exists('DeepSeekAI_Password_Frontend')) {
    class DeepSeekAI_Password_Frontend {
        public function __construct() {
            add_filter('the_content', array($this, 'password_protect_content'), 1);
            add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
            add_action('wp_ajax_deepseek_ai_check_password', array($this, 'ajax_check_password'));
            add_action('wp_ajax_nopriv_deepseek_ai_check_password', array($this, 'ajax_check_password'));
        }

        public function enqueue_assets() {
            if (is_single() || is_page()) {
                wp_enqueue_style('deepseek-ai-password', plugin_dir_url(dirname(__FILE__)) . 'assets/css/password.css', array(), '1.0.0');
                wp_enqueue_script('deepseek-ai-password', plugin_dir_url(dirname(__FILE__)) . 'assets/js/password.js', array('jquery'), '1.0.0', true);
                wp_localize_script('deepseek-ai-password', 'DeepSeekAIPassword', array(
                    'ajax_url' => admin_url('admin-ajax.php'),
                    'nonce' => wp_create_nonce('deepseek-ai-password'),
                    'error_msg' => esc_html__('密码错误，请重试', 'deepseek-ai-summarizer'),
                ));
            }
        }

        public function password_protect_content($content) {
            if (!is_singular()) return $content;
            global $post;
            $post_id = $post->ID;
            // 优先取单篇文章密码
            $password_hash = get_post_meta($post_id, '_deepseek_ai_password_hash', true);
            $has_post_password = !empty($password_hash);
            // 无单篇密码则取全局密码
            if (!$has_post_password) {
                $password_hash = get_option('deepseek_ai_global_password_hash');
            }
            if (empty($password_hash)) return $content; // 没有设置密码

            // 检查cookie
            $cookie_key = 'deepseek_ai_pass_' . $post_id;
            if (!$has_post_password) {
                $cookie_key = 'deepseek_ai_pass_global';
            }
            if (isset($_COOKIE[$cookie_key]) && $_COOKIE[$cookie_key] === '1') {
                return $content;
            }

            // 未验证，显示密码输入框和引导内容
            $hint = get_option('deepseek_ai_password_hint', '请输入访问密码');
            $guide_html = get_option('deepseek_ai_password_guide_html', '如需获取密码，请关注公众号。');
            ob_start();
            ?>
            <div style="max-width:420px;margin:48px auto;padding:36px 28px;background:linear-gradient(135deg,#e0f1ff 0%,#f8f9fa 100%);border-radius:28px;box-shadow:0 8px 32px rgba(0,124,186,0.10),0 1.5px 8px rgba(34,113,177,0.08);text-align:center;backdrop-filter:blur(2px);">
                <div style="margin-bottom:24px;">
                    <svg t="1751306770196" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4699" width="25" height="25"><path d="M305.110204 495.804082c-10.971429 0-19.853061-8.881633-19.853061-19.853062V341.681633C285.257143 216.816327 387.134694 114.938776 512 114.938776c89.338776 0 170.840816 52.767347 206.889796 134.791836 13.061224 29.257143 19.330612 60.081633 19.330612 91.951021 0 10.971429-8.881633 19.853061-19.853061 19.853061-10.971429 0-19.853061-8.881633-19.853061-19.853061 0-26.122449-5.22449-51.722449-16.195919-75.755102-29.257143-67.918367-96.653061-111.281633-170.318367-111.281633-102.922449 0-187.036735 83.591837-187.036735 187.036735v134.269387c0 10.971429-8.881633 19.853061-19.853061 19.853062z" fill="#3D2A03" p-id="4700"></path><path d="M762.253061 909.061224H261.746939c-43.885714 0-79.412245-35.526531-79.412245-79.412244v-294.138776c0-43.885714 35.526531-79.412245 79.412245-79.412245H762.77551c43.885714 0 79.412245 35.526531 79.412245 79.412245v294.138776c-0.522449 43.885714-36.04898 79.412245-79.934694 79.412244z" fill="#F2CB51" p-id="4701"></path><path d="M509.387755 813.453061c-30.82449 0-55.902041-25.077551-55.902041-55.902041v-42.840816c-21.420408-17.240816-33.436735-43.363265-33.436734-71.053061 0-50.677551 41.273469-91.95102 91.95102-91.951021s91.95102 41.273469 91.95102 91.951021c0 30.302041-14.628571 57.991837-38.661224 75.232653v39.183673c0 30.302041-25.077551 55.379592-55.902041 55.379592z" fill="#E5404F" p-id="4702"></path></svg>
                    <div style="font-size:22px;font-weight:700;color:#2271b1;margin-top:10px;letter-spacing:1px;">访问受限</div>
                    <div style="font-size:15px;color:#666;margin-top:4px;">请输入访问密码解锁内容</div>
                </div>
                <form class="deepseek-ai-password-form" data-post-id="<?php echo esc_attr($post_id); ?>" style="width:100%;max-width:320px;margin:0 auto;display:flex;gap:0;align-items:center;justify-content:center;background:rgba(255,255,255,0.7);border-radius:50px;padding:6px 8px 6px 18px;box-shadow:0 1.5px 8px #007cba11;">
                    <input type="password" name="password" autocomplete="off" placeholder="请输入访问密码" style="flex:1 1 0%;padding:14px 0 14px 0;font-size:18px;border:none;border-bottom:2.5px solid #b3c6d9;background:transparent;outline:none;transition:border-color .2s;box-shadow:none;border-radius:0;" onfocus="this.style.borderBottomColor='#2271b1'" onblur="this.style.borderBottomColor='#b3c6d9'" />
                    <button type="submit" style="padding:0 22px;height:44px;display:flex;align-items:center;justify-content:center;font-size:18px;border-radius:50px;background:linear-gradient(90deg,#007cba,#2271b1);font-weight:600;box-shadow:0 2px 8px #007cba22;color:#fff;border:none;cursor:pointer;transition:background .2s;"><svg t="1751306770196" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4699" width="25" height="25"><path d="M305.110204 495.804082c-10.971429 0-19.853061-8.881633-19.853061-19.853062V341.681633C285.257143 216.816327 387.134694 114.938776 512 114.938776c89.338776 0 170.840816 52.767347 206.889796 134.791836 13.061224 29.257143 19.330612 60.081633 19.330612 91.951021 0 10.971429-8.881633 19.853061-19.853061 19.853061-10.971429 0-19.853061-8.881633-19.853061-19.853061 0-26.122449-5.22449-51.722449-16.195919-75.755102-29.257143-67.918367-96.653061-111.281633-170.318367-111.281633-102.922449 0-187.036735 83.591837-187.036735 187.036735v134.269387c0 10.971429-8.881633 19.853061-19.853061 19.853062z" fill="#3D2A03" p-id="4700"></path><path d="M762.253061 909.061224H261.746939c-43.885714 0-79.412245-35.526531-79.412245-79.412244v-294.138776c0-43.885714 35.526531-79.412245 79.412245-79.412245H762.77551c43.885714 0 79.412245 35.526531 79.412245 79.412245v294.138776c-0.522449 43.885714-36.04898 79.412245-79.934694 79.412244z" fill="#F2CB51" p-id="4701"></path><path d="M509.387755 813.453061c-30.82449 0-55.902041-25.077551-55.902041-55.902041v-42.840816c-21.420408-17.240816-33.436735-43.363265-33.436734-71.053061 0-50.677551 41.273469-91.95102 91.95102-91.951021s91.95102 41.273469 91.95102 91.951021c0 30.302041-14.628571 57.991837-38.661224 75.232653v39.183673c0 30.302041-25.077551 55.379592-55.902041 55.379592z" fill="#E5404F" p-id="4702"></path></svg></button>
                </form>
                <div class="deepseek-ai-password-error" style="color:#d32f2f;display:none;margin-top:14px;font-size:15px;"></div>
                <div class="deepseek-ai-password-guide" style="margin-top:22px;">
                    <?php echo wp_kses_post($guide_html); ?>
                </div>
                <style>@media (max-width:600px){div[style*='max-width:420px']{padding:18px 4vw !important;}form.deepseek-ai-password-form{max-width:98vw !important;}}</style>
            </div>
            <script>document.querySelector('.deepseek-ai-password-form input[name="password"]').addEventListener('keydown',function(e){if(e.key==='Enter'){e.preventDefault();this.form.querySelector('button[type="submit"]').click();}});</script>
            <?php
            return ob_get_clean();
        }

        public function ajax_check_password() {
            check_ajax_referer('deepseek-ai-password', 'nonce');
            $post_id = intval($_POST['post_id']);
            $password = isset($_POST['password']) ? sanitize_text_field($_POST['password']) : '';
            $password_hash = get_post_meta($post_id, '_deepseek_ai_password_hash', true);
            $has_post_password = !empty($password_hash);
            if (!$has_post_password) {
                $password_hash = get_option('deepseek_ai_global_password_hash');
            }
            if (empty($password_hash)) {
                wp_send_json_error(array('msg' => '未设置密码'));
            }
            if (password_verify($password, $password_hash)) {
                // 设置cookie，1天有效
                $cookie_key = $has_post_password ? ('deepseek_ai_pass_' . $post_id) : 'deepseek_ai_pass_global';
                setcookie($cookie_key, '1', time() + 86400, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
                wp_send_json_success();
            } else {
                wp_send_json_error(array('msg' => '密码错误'));
            }
        }
    }
    new DeepSeekAI_Password_Frontend();
} 